package model

import (
	"crypto/md5"
	"fmt"
	"gorm.io/gorm"
	"shorturl/global"
	"time"
)

//CREATE TABLE `ky_user` (
//`id` int NOT NULL AUTO_INCREMENT COMMENT '管理员ID',
//`username` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户名',
//`password` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '密码',
//`salt` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '盐',
//`phone` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '手机号',
//`role` tinyint NOT NULL COMMENT '角色',
//`token` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '令牌',
//`create_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
//`update_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
//`delete_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
//PRIMARY KEY (`id`) USING BTREE,
//KEY `token` (`token`) USING BTREE
//) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='管理用户表';

type User struct {
	Id       int            `json:"id" gorm:"column:id;type:int(11);not null;primaryKey;autoIncrement"`
	Username string         `json:"username" gorm:"column:username;type:varchar(32);not null"`
	Password string         `json:"password" gorm:"column:password;type:varchar(32);not null"`
	Salt     string         `json:"salt" gorm:"column:salt;type:varchar(10);not null"`
	Phone    string         `json:"phone" gorm:"column:phone;type:varchar(15);not null"`
	Role     int            `json:"role" gorm:"column:role;type:int(1);default:null"`
	Token    string         `json:"token" gorm:"column:token;type:varchar(255);default:null"`
	CreateAt LocalTime      `json:"create_at" gorm:"column:create_at;type:TIMESTAMP;default:CURRENT_TIMESTAMP;<-:create;default:null"`
	UpdateAt LocalTime      `json:"update_at" gorm:"column:update_at;type:TIMESTAMP;default:CURRENT_TIMESTAMP  on update current_timestamp;default:null"`
	DeleteAt gorm.DeletedAt `json:"delete_at" gorm:"column:delete_at;type:datetime;default:null"`
}

// CreateToken 生成token
func (user User) CreateToken(uid int) string {
	//使用md5 uid+当前时间戳
	m := md5.New()
	m.Write([]byte(fmt.Sprintf("%d%d", uid, time.Now().Unix())))
	token := fmt.Sprintf("%x", m.Sum(nil))
	return token
}

// FindUserByID 查找用户
func (user User) FindUserByID(users []User, targetID int) *User {
	for i := range users {
		if users[i].Id == targetID {
			return &users[i]
		}
	}
	return nil
}

// GetUserById 根据id查询用户
func (user User) GetUserById(id int) (*User, error) {
	err := global.Db.Where("id = ?", id).First(&user).Error
	if err != nil {
		return nil, err
	}
	return &user, nil
}

// GetUserMapDict 查询出用户id name转为map字典
func (user User) GetUserMapDict(users []User) map[int64]string {
	userMap := make(map[int64]string)
	for _, user := range users {
		userMap[int64(user.Id)] = user.Username
	}
	return userMap
}

// GetUserByToken 根据token查询用户
func (user User) GetUserByToken(token string) (*User, error) {
	err := global.Db.Where("token = ?", token).First(&user).Error
	if err != nil {
		return nil, err
	}
	return &user, nil
}
